package com.tq.portal.connector.gitlab;


import com.tq.portal.connector.gitlab.model.dto.GitLabActivityDTO;
import com.tq.portal.connector.gitlab.model.dto.GitLabCommitDTO;
import com.tq.portal.connector.gitlab.model.dto.GitLabProjectDTO;
import com.tq.web.model.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * GitLab
 *
 * @author TqCoder
 * @since 2.0.0
 */
@RestController
@RequestMapping("gitlab")
@RequiredArgsConstructor
public class GitLabController {

    private final GitLabClient gitLabClient;

    /**
     * 获取所有项目
     */
    @GetMapping("/projects")
    public Result<List<GitLabProjectDTO>> getAllProjects() {
        return Result.ok(gitLabClient.getAllProjects());
    }

    /**
     * 获取活动列表
     *
     * @param page    页码，默认1
     * @param perPage 每页数量，默认20
     * @param after   起始时间（可选），格式：yyyy-MM-dd HH:mm:ss，如 2024-01-01 00:00:00
     * @param before  结束时间（可选），格式：yyyy-MM-dd HH:mm:ss，如 2024-12-31 23:59:59
     * @return 活动列表
     */
    @GetMapping("/activities")
    public Result<List<GitLabActivityDTO>> getActivities(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "20") Integer perPage,
            @RequestParam(required = false) String after,
            @RequestParam(required = false) String before) {
        return Result.ok(gitLabClient.getActivities(page, perPage, after, before));
    }

    /**
     * 获取项目提交记录（包括子模块）
     *
     * @param projectId 项目ID
     * @param branch    分支名称（可选），默认为项目的默认分支
     * @param since     起始时间（可选），格式：yyyy-MM-dd HH:mm:ss，如 2024-01-01 00:00:00
     * @param until     结束时间（可选），格式：yyyy-MM-dd HH:mm:ss，如 2024-12-31 23:59:59
     * @return 提交记录列表
     */
    @GetMapping("/projects/{projectId}/commits")
    public Result<List<GitLabCommitDTO>> getProjectCommitsWithSubmodules(
            @PathVariable Integer projectId,
            @RequestParam(required = false) String branch,
            @RequestParam(required = false) String since,
            @RequestParam(required = false) String until) {
        return Result.ok(gitLabClient.getProjectCommitsWithSubmodules(projectId, branch, since, until));
    }
}
